package cn.edu.cug.cs.gtl.apis.ui.datake.catalogtree;

import cn.edu.cug.cs.gtl.apis.ui.AbstractApplication;
import cn.edu.cug.cs.gtl.apis.ui.datake.DatakeApp;
import cn.edu.cug.cs.gtl.apis.ui.datake.DatakeFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class CatalogLoginDialog extends JDialog {

    private JTextField ipField;
    private JTextField dbNameField;
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JButton connectButton;

    private AbstractApplication theApp;

    public CatalogLoginDialog(AbstractApplication theApp) {
//        super(theApp.getMainFrame(), "PostgreSQL Connection", true);
        this.theApp = theApp;
        initComponents();
    }

    private void initComponents() {
        ipField = new JTextField();
        dbNameField = new JTextField();
        usernameField = new JTextField();
        passwordField = new JPasswordField();
        connectButton = new JButton("Connect");

        ipField.setText("202.114.198.141");
        dbNameField.setText("geospacex");
        usernameField.setText("gis");
        passwordField.setText("gis");

        connectButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onConnect();
            }
        });

        JPanel panel = new JPanel(new GridLayout(5, 2));
        panel.add(new JLabel("Database IP:"));
        panel.add(ipField);
        panel.add(new JLabel("Database Name:"));
        panel.add(dbNameField);
        panel.add(new JLabel("Username:"));
        panel.add(usernameField);
        panel.add(new JLabel("Password:"));
        panel.add(passwordField);

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(connectButton);

        add(panel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void onConnect() {
        String ip = ipField.getText();
        String dbName = dbNameField.getText();
        String username = usernameField.getText();
        char[] passwordChars = passwordField.getPassword();
        String password = new String(passwordChars);

        // JDBC connection URL
        String url = "jdbc:postgresql://" + ip + "/" + dbName;

        try (Connection connection = DriverManager.getConnection(url, username, password)) {
            // Connection successful
            JTree tree = getDatakeFrame().getCatalogPanel().getTreeComponent();
            dispose(); // Close the dialog after successful connection
            JTreeTools.createTree(tree);
        } catch (SQLException ex) {
            // Connection failed
            JOptionPane.showMessageDialog(this, "Connection failed:\n" + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    public DatakeFrame getDatakeFrame() {
        return (DatakeFrame) (((DatakeApp)theApp).getMainFrame());
    }


}

